function SlideSwipe (el, options) {
	this.el = $(el)
	this.elName = el
	this.index = 1
	this.length = 0
	this.size = 8
	this.itemWidth = 120
	options.data = options.data || []
	this.init(options);
}

/**
 * SlideSwiper 初始化 
 * @param  {[type]} options { data: [] }
 * @return {[type]}         [description]
 */
SlideSwipe.prototype.init = function (options) {
	this.renderList(options.data);
}

SlideSwipe.prototype.renderList = function (data) {
	this.index = 1
	this.length = data.length || 0
	var $content = this.el.find('.slide-content');
	$content.empty();
	var str = '<div class="slide-scroll" style="width:'+ this.itemWidth * this.length +'px">';
	data.forEach(function (item) {
		str += '<div class="slide-item" data-value="'+ item.value +'" style="width: '+ this.itemWidth +'px;">'+ item.name +'</div>';
	}.bind(this));
	str += '</div>';
	$content.append(str);
	this.events();
	this.el.find('.slide-item').eq(0).click();
}

SlideSwipe.prototype.renderOptionList = function (data) {
	var list = [
		{ point: '优势点', type: '1匹', price: '2000~2500' },
		{ point: '优势点', type: '1匹', price: '1500~2000' },
		{ point: '优势点', type: '1匹', price: '2000~2500' },
		{ point: '优势点', type: '1匹', price: '1500~2000' },
		{ point: '优势点', type: '1匹', price: '2000~2500' },
		{ point: '优势点', type: '1匹', price: '1500~2000' },
		{ point: '改善点', type: '2匹', price: '3000~3500' },
		{ point: '机会点', type: '1.5匹', price: '2000~2500' }
	]
	var advantageList = list.filter(function (item) {
		if (item.point === '优势点') {
			return item;
		}
	});
	advantageList = advantageList || [];
	var improveList = list.filter(function (item) {
		if (item.point === '改善点') {
			return item;
		}
	});
	improveList = improveList || [];
	var chanceList = list.filter(function (item) {
		if (item.point === '机会点') {
			return item;
		}
	});
	chanceList = chanceList || [];

	renderList('advantageList', advantageList);
	renderList('improveList', improveList);
	renderList('chanceList', chanceList);

	function renderList (listName, list) {
		var $list = $('#' + listName);
		var bg = listName === 'advantageList' ? 'bg-blue' : listName === 'improveList' ? 'bg-red' : listName === 'chanceList' ? 'bg-green' : '';
		var str = ''
		$list.empty();
		list.forEach(function(item) {
			// var value = item.type + '&' + item.price;
			var name = item.type + ' & ' + item.price;
			str += '<li class="slide-option '+ bg +'" data-seg="'+ item.price +'" data-type="'+ item.type +'" data-bg="'+ bg +'">'+ name +'</li>';
		});
		$list.append(str);
	}
}

SlideSwipe.prototype.events = function () {
	// 上一页
	$(document).off('click', this.elName + ' .slide-prev-btn', move);
	$(document).on('click', this.elName + ' .slide-prev-btn', move.bind(this, 'prev'));
	// 下一页
	$(document).off('click', this.elName + ' .slide-next-btn', move);
	$(document).on('click', this.elName + ' .slide-next-btn', move.bind(this, 'next'));
	function move (type) {
		var scroll = this.el.find('.slide-scroll');
		var totalPage = Math.ceil(this.length / this.size);
		var array = scroll.css('transform').substring(7).trim().split(',')
		var current = array.length > 2 ? parseFloat(array[4]) : 0
		if (this.length > 0) {
			var distance = this.size * this.itemWidth;
			if (type === 'prev') {
				if (this.index < totalPage) {
					scroll.css({ transform: 'translateX('+ (current - distance) +'px)' });
					this.index++;
				} else if (this.index === totalPage) {
					scroll.css({ transform: 'translateX('+ 0 +'px)' });
					this.index = 1;
				}
			} else if (type === 'next') {
				if (this.index > 1) {
					scroll.css({ transform: 'translateX('+ (current + distance) +'px)' });
					this.index--;
				} else if (this.index === 1) {
					scroll.css({ transform: 'translateX('+ (totalPage - 1) * -distance +'px)' })
					this.index = totalPage;
				}
			}
		}
	}

	// 导航点击
	$(document).off('click', this.elName + ' .slide-item', navClick);
	$(document).on('click', this.elName + ' .slide-item', navClick.bind(this));
	function navClick (event) {
		var target = event.target;
		var type = $(target).data('value');
		this.el.find('.slide-item').removeClass('active');
		$(target).addClass('active');
		this.renderOptionList();
	}

	// 选项点击
	$(document).off('click', this.elName + ' .slide-option', optionClick);
	$(document).on('click', this.elName + ' .slide-option', optionClick.bind(this));

	function optionClick (event) {
		var target = event.target;
		var seg = $(target).data('seg');
		var type = $(target).data('type');
		var bg = $(target).data('bg');
		this.el.find('.slide-option').removeClass('active');
		$(target).addClass('active');
		$('#shareTable td').attr('class', '');
		// $('#shareTable td').removeClass('bg-red').removeClass('bg-blue').removeClass('bg-green')
		$('#shareTable td').each(function (index, item) {
			var itemSeg = $(item).data('seg');
			var itemType = $(item).data('type');
			if (seg === itemSeg && type === itemType) {
				$(item).addClass(bg)
				console.log(item)
			}
		})
	}
}